package com;

import cn.hutool.crypto.SecureUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.*;

public class CarsTest {


    public static void main(String[] args) throws Exception {
        String url="http://xxxcarsadm.ximgs.net";

//        String url="http://localhost:9017";
        String rest = httpGet(url+"/api/open/sync/all/12ss3456");

        System.out.println(rest);
    }

    public static String httpGet(String url) throws Exception {
        String secret = "go2CN|20220119wearethebest";
        String nonce = UUID.randomUUID().toString();
        long timestamp = System.currentTimeMillis();
        String key = SecureUtil.sha1(secret + nonce + timestamp);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet post = new HttpGet(url);
        post.setHeader("sign", key);
        post.setHeader("nonce", nonce);
        post.setHeader("timestamp", timestamp + "");
        String response = client.execute(post, new ResponseHandler<String>() {
            @Override
            public String handleResponse(HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity, "utf-8") : null;
                } else {
                    System.out.println(response);
                }
                System.out.println(status);
                return null;
            }
        });
        return response;
    }
}
